/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.web.rebate;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.components.FeignManager;
import com.sigmatrix.h5.entity.DisDealerAcct;
import com.sigmatrix.h5.entity.Enterprise;
import com.sigmatrix.h5.service.DisDealerAcctService;
import com.sigmatrix.h5.service.EnterpriseService;
import com.sigmatrix.h5.service.WeChatSmallProgramService;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.utils.Constants;
import com.sigmatrix.h5.web.BaseController;
import com.sigmatrix.h5.web.Result;
import com.sigmatrix.h5.web.args.EnterpriseParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: DisInterpriseController
 * @Description:公用接口，未走拦截器
 * @author wjwei
 * @date 2018年3月7日 上午10:40:22
 */
@Api(tags = "企业相关")
@RestController
@RequestMapping("/enterprise")
public class DisEnterpriseController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(DisEnterpriseController.class);

	@Autowired
	private FeignManager feignManager;

	@Autowired
	private EnterpriseService enterpriseService;

	@Autowired
	private DisDealerAcctService disDealerAcctService;
	
	@Autowired
	private WeChatSmallProgramService weChatSmallProgramService;

	/**
	 * 
	 * @Title: getEnterpriseByPromotionCode
	 * @Description:根据推广码查询企业信息
	 * @return Result<EnterpriseParam>
	 * @author wjwei
	 * @date 2018年3月7日 上午10:51:37
	 * @param promotionCode
	 * @return
	 */
	@ApiOperation("根据推广码查询企业信息")
	@GetMapping("/get")
	public Result<EnterpriseParam> getEnterpriseByPromotionCode(String promotionCode) {
		// 业务员
		Enterprise enterprise = null;
		try {
			DisDealerAcct disDealerAcct = disDealerAcctService.findByPromotionCode(promotionCode);
			enterprise = null;
			if (disDealerAcct != null) {
				enterprise = enterpriseService.findById(disDealerAcct.getEnterpriseId());
			} else {
				LOGGER.info("通过经销商推广码查询企业信息开始：参数=" + promotionCode);
				// 经销商
				enterprise = feignManager.getEnterpriseBy(promotionCode);
				LOGGER.info("通过经销商推广码查询企业信息结果=" + JSONObject.toJSONString(enterprise));
			}
		} catch (Exception e) {
			LOGGER.error("查询失败", e.getLocalizedMessage());
			return error("查询失败");
		}
		if (CommonUtils.isEmpty(enterprise)) {
			return error("没有查询到企业");
		}
		if (StringUtils.isBlank(enterprise.getCode())) {
			return error("无效的企业，没有编码");
		}
		if (enterprise.getStatus() != Constants.STATUS_ENABLE) {
			return error("无效的企业，已经禁用");
		}
		EnterpriseParam param = new EnterpriseParam();
		param.seteCode(enterprise.getCode());
		param.seteId(enterprise.getBasicModuleId());
		return ok(param);
	}
	/**
	 * 
	 *@Title: getOpenIdByJsCode
	 *@Description:根据jsCode获取小程序的openId
	 *@return Result<String>
	 *@author wjwei
	 *@date 2018年3月9日 下午1:41:43
	 *@param jsCode
	 *@return
	 */
	@ApiOperation("根据jsCode获取小程序的openId")
	@GetMapping("/openId")
	public Result<String> getOpenIdByJsCode(String jsCode) {
		return ok(weChatSmallProgramService.getOpenId(jsCode));
	}
}
